package com.chainyoung.trademarket.ui;

import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.text.InputFilter;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.chainyoung.common.base.BaseActivity;
import com.chainyoung.common.bean.UserInfo;
import com.chainyoung.common.common_widget.TimeCount;
import com.chainyoung.common.di.AppComponent;
import com.chainyoung.common.event.BaseEvent;
import com.chainyoung.common.utils.CommonUtil;
import com.chainyoung.common.utils.StringUtil;
import com.chainyoung.common.utils.ToastUtil;
import com.chainyoung.trademarket.R;
import com.chainyoung.trademarket.di.component.DaggerRegisterComponent;
import com.chainyoung.trademarket.di.module.RegisterModule;
import com.chainyoung.trademarket.mvp.contract.RegisterContract;
import com.chainyoung.trademarket.mvp.presenter.RegisterPresenter;

import org.simple.eventbus.EventBus;

import java.util.TreeMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static android.text.InputType.TYPE_CLASS_NUMBER;
import static android.text.InputType.TYPE_CLASS_PHONE;
import static android.text.InputType.TYPE_CLASS_TEXT;
import static android.text.InputType.TYPE_TEXT_FLAG_MULTI_LINE;
import static com.chainyoung.trademarket.ui.LoginActivity.FROM_EMAIL;

public class RegisterActivity extends BaseActivity<RegisterPresenter> implements RegisterContract.View {


    @BindView(R.id.et_mobile)
    EditText etMobile;
    @BindView(R.id.et_pwd)
    EditText etPwd;
    @BindView(R.id.et_confirm_pwd)
    EditText etConfirmPwd;
    @BindView(R.id.et_validate_code)
    EditText etValidateCode;
    @BindView(R.id.tv_send_code)
    TextView tvSendCode;
    @BindView(R.id.et_invite_code)
    EditText etInviteCode;
    @BindView(R.id.btn_register)
    Button btnRegister;


    private boolean isEmailRegister;
    private String accountNo, pwd, confirmPwd, code, inviteCode;

    private TimeCount timeCount;


    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerRegisterComponent.builder()
                .appComponent(appComponent)
                .registerModule(new RegisterModule(this))
                .build()
                .inject(this);
    }


    @Override
    protected void initUI() {
        setCurrentTitle(getString(R.string.register));
        mBarRightTextView.setTextColor(ContextCompat.getColor(mContext, R.color._4486f5));
        isEmailRegister = getIntent().getBooleanExtra(FROM_EMAIL, false);
        etMobile.setHint(isEmailRegister ? getString(R.string.pls_fill_email) : getString(R.string.pls_fill_mobile_no));
        etMobile.setInputType(isEmailRegister ? TYPE_CLASS_TEXT : TYPE_CLASS_NUMBER);
        etMobile.setFilters(isEmailRegister ? new InputFilter[]{new InputFilter.LengthFilter(50)} : new InputFilter[]{new InputFilter.LengthFilter(11)});
        showAndSetBarRightText(isEmailRegister ? getString(R.string.goto_mobile_register) : getString(R.string.goto_email_register));
        timeCount = new TimeCount(60000, 1000, tvSendCode);
    }

    @Override
    public void onLeftViewClicked(View view) {
        super.onLeftViewClicked(view);
        launch(LoginActivity.class);
        finish();
    }

    @Override
    public void onRightTxtClicked(View v) {
        super.onRightTxtClicked(v);
        Bundle bundle = new Bundle();
        bundle.putBoolean(FROM_EMAIL, !isEmailRegister);
        launch(RegisterActivity.class, bundle);
        finish();
    }

    @Override
    protected void initData() {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_mobile_register;
    }


    @Override
    public void onRegisterSuccess(UserInfo userInfo) {
        EventBus.getDefault().post(new BaseEvent<>(BaseEvent.EventCode.LOGIN_IN, userInfo));
        launch(MainMarketActivity.class);
        finish();
    }


    @Override
    public void onSendCodeSuccess(String object) {
        timeCount.start();
    }


    @OnClick({R.id.btn_register, R.id.tv_send_code})
    void OnClick(View v) {
        switch (v.getId()) {
            case R.id.btn_register:
                if (checkEmpty(etMobile) || checkEmpty(etPwd) || checkEmpty(etConfirmPwd) ||
                        checkEmpty(etValidateCode)) {
                    return;
                }
                if (isEmailRegister && !StringUtil.isEmail(StringUtil.viewGetText(etMobile))) {
                    ToastUtil.showToast("请输入正确的邮箱账号");
                    return;
                }

                if (!isEmailRegister && !StringUtil.isMobileNumber(StringUtil.viewGetText(etMobile))) {
                    ToastUtil.showToast("请输入正确的手机号");
                    return;
                }

                if (!StringUtil.isPwdValidate(StringUtil.viewGetText(etPwd))) {
                    ToastUtil.showToast(getString(R.string.pls_input_correct_pwd));
                    return;
                }
                if (!StringUtil.viewGetText(etPwd).equals(StringUtil.viewGetText(etConfirmPwd))) {
                    ToastUtil.showToast(getString(R.string.pwd_not_same));
                    return;
                }


                accountNo = StringUtil.viewGetText(etMobile);
                pwd = StringUtil.viewGetText(etPwd);
                confirmPwd = StringUtil.viewGetText(etConfirmPwd);
                code = StringUtil.viewGetText(etValidateCode);
                inviteCode = StringUtil.viewGetText(etInviteCode);

                TreeMap<String, Object> params = new TreeMap<>();

                params.put("account", accountNo);
                params.put("password", pwd);
                params.put("password_confirm", confirmPwd);
                params.put("verify_code", code);
                params.put("invit", inviteCode);
                mPresenter.getRegister(params);

                break;
            case R.id.tv_send_code:
                if (CommonUtil.isFastDoubleClick()) {
                    return;
                }
                if (StringUtil.viewGetTextIsEmpty(etMobile)) {
                    ToastUtil.showToast(String.valueOf(etMobile.getHint()));
                    return;
                }

                if (isEmailRegister && !StringUtil.isEmail(StringUtil.viewGetText(etMobile))) {
                    ToastUtil.showToast("请输入正确的邮箱账号");
                    return;
                }

                if (!isEmailRegister && !StringUtil.isMobileNumber(StringUtil.viewGetText(etMobile))) {
                    ToastUtil.showToast("请输入正确的手机号");
                    return;
                }


                TreeMap<String, Object> map = new TreeMap<>();
                String accountNo = StringUtil.viewGetText(etMobile);

                map.put("account", accountNo);
                map.put("type", "register");
                mPresenter.sendCode(map);

                break;


        }
    }

    private boolean checkEmpty(TextView view) {
        if (StringUtil.viewGetTextIsEmpty(view)) {
            ToastUtil.showToast(String.valueOf(view.getHint()));
            return true;
        }
        return false;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (timeCount != null) {
            timeCount.cancel();
        }

    }


}
